<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\contract;

use app\common\dao\BaseDao;
use app\common\model\contract\ContractBasicModel;

class ContractBasicDao extends BaseDao
{

    /**
     * @return ContractBasicModel
     */
    protected function getModel(): string
    {
        return ContractBasicModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['room_id']) && $where['room_id'] !== '', function ($query) use ($where) {
                $query->where('room_id', intval($where['room_id']));
            })
            ->when(isset($where['sign_type']) && $where['sign_type'] !== '', function ($query) use ($where) {
                $query->where('sign_type', intval($where['sign_type']));
            })
            ->when(isset($where['sign_audit']) && $where['sign_audit'] !== '', function ($query) use ($where) {
                $query->where('sign_audit', intval($where['sign_audit']));
            })
            ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
                $query->where('status', intval($where['status']));
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('bill_prompt|remark|end_remark|number', '%' . trim($where['keywords']) . '%');
            })
            ->when(isset($where['time']) && $where['time'] !== '', function ($query) use ($where) {
                $this->timeSearchBuild($query, $where['time'], 'start_date');
            });
    }

}
